package it.ap.sa.acqua.tank.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import it.ap.sa.acqua.dao.beans.LivestockBean;
import it.ap.sa.acqua.dao.beans.SearchTankFilter;
import it.ap.sa.acqua.dao.beans.TTankBean;
import it.ap.sa.acqua.dao.beans.TTankTypeAlertBean;
import it.ap.sa.acqua.shared.web.AbstractCtrl;
import it.ap.sa.acqua.tank.service.ITankService;
import it.ap.sa.core.dao.beans.search.ResponseData;
import it.ap.sa.core.utils.LogUtil;

/**
 * Controller per la gestione delle vasche
 * 
 * @author Alessandro Paolillo
 *
 */
@Controller(value = "Tank Controller")
@RequestMapping("/tankCtrl")
public class TankController extends AbstractCtrl {

	private static final Logger logger = LoggerFactory.getLogger(TankController.class.getName());
	
	@Autowired
	private ITankService tankService;
	
	@Override
	@RequestMapping(value = "/tankDetail")
	public ModelAndView loadPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathTankPages() + "tankDetail");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	@RequestMapping(value = "/tankPrefer")
	public ModelAndView loadPreferPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadPreferPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathTankPages() + "tankPrefer");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	@RequestMapping(value = "/myTank")
	public ModelAndView loadMyTankPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String    idLog     = "loadMyTankPage";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		try{
			ModelAndView  mav      		= new ModelAndView(this.getPathTankPages() + "myTank");
			return mav;
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
	}
	
	/**
	 */
	@RequestMapping(value = "/getTank.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getTank(HttpServletRequest request, @RequestParam(name="id") Long id) throws Exception {
		
		String    idLog     = "getTank";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			TTankBean bean = tankService.getTank( id );
			result = this.getResponseDataByObject(bean, locale);
		}catch(Exception e){
			logger.error("Error in getTank", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getTanks.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getTanks(HttpServletRequest request) throws Exception {
		
		String    idLog     = "getTanks";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TTankBean> listBean = tankService.getTanks( );
			result = this.getResponseDataByList(listBean, locale);
		}catch(Exception e){
			logger.error("Error in getTanks", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/searchTank.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData searchTank(HttpServletRequest request, 
			@RequestBody SearchTankFilter searchTankFilter ) throws Exception {
		
		String    idLog     = "searchTank";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TTankBean> resultData = tankService.searchTank( searchTankFilter );
			result = this.getResponseDataByList(resultData, locale);
			
			if ( searchTankFilter.isPaginate() ) {
				Long iTotalRecords = tankService.searchCountTank( searchTankFilter );
				result.setiTotalRecords( iTotalRecords.intValue() );
			}
		}catch(Exception e){
			logger.error("Error in searchTank", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/myTanks.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData myTanks(HttpServletRequest request, 
			@RequestBody SearchTankFilter searchTankFilter ) throws Exception {
		
		String    idLog     = "myTanks";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TTankBean> resultData = tankService.searchTank( searchTankFilter );
			result = this.getResponseDataByList(resultData, locale);
			
			if ( searchTankFilter.isPaginate() ) {
				Long iTotalRecords = tankService.searchCountTank( searchTankFilter );
				result.setiTotalRecords( iTotalRecords.intValue() );
			}
		}catch(Exception e){
			logger.error("Error in myTanks", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/saveTank.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData saveTank(HttpServletRequest request, 
			@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "saveTank";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			TTankBean resultData = tankService.saveTank( tankBean );
			result = this.getResponseDataByObject(resultData, locale);
		}catch(Exception e){
			logger.error("Error in saveTank", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/deleteTank.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData deleteTank(HttpServletRequest request, 
			@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "deleteTank";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			tankService.deleteTank( tankBean );
			result = this.getResponseDataByObject(tankBean, locale);
		}catch(Exception e){
			logger.error("Error in deleteTank", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getTankTypes.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getTankTypes(HttpServletRequest request) throws Exception {
		
		String    idLog     = "getTankTypes";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<TTankTypeAlertBean> resultData = tankService.getTankTypes();
			result = this.getResponseDataByList(resultData, locale);
		}catch(Exception e){
			logger.error("Error in getTankTypes", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/***************************************************************************************************	DETTAGLIO VASCA	************************/
	/**
	 */
	@RequestMapping(value = "/getTask.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getTask(HttpServletRequest request, 
				@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "getTask";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<LivestockBean> resultData = new ArrayList<LivestockBean>();
			result = this.getResponseDataByList(resultData, locale);
		}catch(Exception e){
			logger.error("Error in getTask", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getLivestock.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getLivestock(HttpServletRequest request, 
				@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "getLivestock";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<LivestockBean> resultData = tankService.getLivestock( tankBean.getId() );
			result = this.getResponseDataByList(resultData, locale);
		}catch(Exception e){
			logger.error("Error in getLivestock", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/saveLivestock.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData saveLivestock(HttpServletRequest request, 
				@RequestBody LivestockBean livestockBean ) throws Exception {
		
		String    idLog     = "saveLivestock";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			tankService.saveLivestock( livestockBean );
			result = this.getResponseDataByNull(locale);
		}catch(Exception e){
			logger.error("Error in saveLivestock", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/deleteLivestock.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData deleteLivestock(HttpServletRequest request, 
				@RequestBody LivestockBean livestockBean ) throws Exception {
		
		String    idLog     = "deleteLivestock";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		ResponseData result = null;
		try{
			
			tankService.deleteLivestock( livestockBean );
		}catch(Exception e){
			logger.error("Error in deleteLivestock", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getTankAnalisys.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getTankAnalisys(HttpServletRequest request, 
				@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "getTankAnalisys";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<LivestockBean> resultData = tankService.getLivestock( tankBean.getId() );
			result = this.getResponseDataByList(resultData, locale);
		}catch(Exception e){
			logger.error("Error in getTankAnalisys", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
	
	/**
	 */
	@RequestMapping(value = "/getSocialNetwork.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getSocialNetwork(HttpServletRequest request, 
				@RequestBody TTankBean tankBean ) throws Exception {
		
		String    idLog     = "getSocialNetwork";
		StopWatch stopWatch = LogUtil.startStopWatch(idLog);
		logger.info("Start " + idLog);
		Locale       locale    = request.getLocale();
		ResponseData result = null;
		try{
			
			List<LivestockBean> resultData = tankService.getLivestock( tankBean.getId() );
			result = this.getResponseDataByList(resultData, locale);
		}catch(Exception e){
			logger.error("Error in getSocialNetwork", e);
			result = this.getErrorResponseData(request);
		}finally{
			logger.info(LogUtil.endStopWatch(stopWatch));
		}
		return result;
	}
}
